#include "rbmpgeneratorasync.h"
#include "imagematrix.h"
#include "eightlayerim.h"
#include <iostream>


RBMPGeneratorAsync::RBMPGeneratorAsync()
{
	randomize();
}

void RBMPGeneratorAsync::randomize()
{
	for (int i = 0; i < BITS_PER_PIXEL; ++i)
	{
		period[i] = 1 + (rand() % (MCOLS-1));
		sequence[i].clear();
		for (int valueCounter = 0; valueCounter < period[i]; ++valueCounter)
			sequence[i].push_back( (rand()%2)==1 ); //the bit[i] is either 0 or 1 anywhere in the sequence.
	}
}

void RBMPGeneratorAsync::draw (std::string filename)
{
	try
	{
		//create 8 imagematrices and combine them to an ELIM
		ImageMatrix bitMatrix[BITS_PER_PIXEL];
		int sourceData[MROWS][MCOLS];
		for (int bitCounter = 0; bitCounter < BITS_PER_PIXEL; ++ bitCounter)
		{
			int bitInSequence = 0;
			for (int row = 0; row < MROWS; ++row)
				for (int col = 0; col < MCOLS; ++col)
					sourceData[row][col] = sequence[bitCounter][bitInSequence++ % (int) period[bitCounter]];

			bitMatrix[bitCounter].load(sourceData);
		}
		EightLayerIM image(bitMatrix, BITS_PER_PIXEL);
		image.saveToBlankBMP(filename, BITS_PER_PIXEL); 
	}
	catch (exception e)
	{
		std::cerr << e.what();
	}

}

